// pages/authentication/shop/index.js
const rules = require('../../../utils/util.js')
const uploadImage = require("../../../utils/uploadImg/uploadImg.js");
const service = require('../../../service/authentication')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    charter:[],
    openaccount:[],
    idcardfront:[],
    idcardback:[],

    name:"",
    Contact:"",
    phone:"",
    nameErrorText:"",
    ContactErrorText:"",
    phoneErrorText:""
  },
  blurEvent(e) {
    this.setData({
      nameErrorText: '',
      name: e.detail.value
    });
    // if (e.detail.value == '') {
    //   this.setData({
    //     nameErrorText: '公司名称不能为空！',
    //   });
    // } else {
    //   this.setData({
    //     nameErrorText: '',
    //     name: e.detail.value
    //   });
    // }
  },
  blurEventContact(e) {

    if (e.detail.value == '') {
      this.setData({
        ContactErrorText: '联系人不能为空！',
      });
    } else {
      this.setData({
        ContactErrorText: '',
        Contact: e.detail.value
      });
    }
  },
  blurEventphone(e) {

    if (e.detail.value == '') {
      this.setData({
        phoneErrorText: '联系人手机号不能为空！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
        phone: e.detail.value
      });
    }
  },
  onChangephone(e) {
    //console.log(e.detail)
    if (!rules.regPhone(e.detail)) {
      this.setData({
        phoneErrorText: '手机号格式有误！',
      });
    } else {
      this.setData({
        phoneErrorText: '',
      });
    }
  },
  afterRead: function (e) {
    // console.log(e);
    var list = this.data.charter;

    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      charter: list,

    });
  },
  afterReadopen: function (e) {
    // console.log(e);
    var list = this.data.openaccount;

    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      openaccount: list,

    });
  },
  afterReadidcardf: function (e) {
    // console.log(e);
    var list = this.data.idcardfront;

    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      idcardfront: list,

    });
  },
  afterReadidcardb: function (e) {
    // console.log(e);
    var list = this.data.idcardback;

    list.push({
      url: e.detail.file.path,
    });
    this.setData({
      idcardback: list,

    });
  },
  deletephoto: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.charter;
    fileList.splice(index, 1);
    this.setData({
      charter: fileList,
    });
  },
  deletephotoopen: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.openaccount;
    fileList.splice(index, 1);
    this.setData({
      openaccount: fileList,
    });
  },
  deletephotoidcardf: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.idcardfront;
    fileList.splice(index, 1);
    this.setData({
      idcardfront: fileList,
    });
  },
  deletephotoidcardb: function (event) {
    const {
      index,
      name
    } = event.detail;
    const fileList = this.data.idcardback;
    fileList.splice(index, 1);
    this.setData({
      idcardback: fileList,
    });
  },
  submit(){
    // if (this.data.name == "") {
    //   this.setData({
    //     nameErrorText: '公司名称不能为空！',
    //   });

    //   return
    // }
    if (this.data.Contact == "") {
      this.setData({
        ContactErrorText: '联系人不能为空！',
      });

      return
    }
    if (this.data.phone == "") {
      this.setData({
        phoneErrorText: '联系人手机号不能为空！',
      });

      return
    }
    wx.showLoading({
      title: '加载中...',
    })
    service.addbusiness({ 
      "customerid": wx.getStorageSync('userInfo').id,
      //公司名称
      "name":this.data.name,
      //联系人
      "contactsName":this.data.Contact,
      //联系人手机号
      "contactsPhone":this.data.phone,
      //营业执照
      "businessLicense":this.data.charter[0]?this.data.charter[0].url:'',
      //开户许可
      "openPermission":this.data.openaccount[0]?this.data.openaccount[0].url:'',
      //身份证正面照片
      "legalIdcard":this.data.idcardfront[0]?this.data.idcardfront[0].url:'',
      //身份证反面照片
      "legalIdcardRebellion":this.data.idcardback[0]?this.data.idcardback[0].url:''
    
  },res=>{
  
   if(res.code=="00000"){
     wx.redirectTo({
       url: '/pages/authentication/index',
     })
   }else{
     wx.showToast({
       title: '请重新提交',
       icon:'none',
       duration:5000
     })
   }
   wx.hideLoading()
  })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})